﻿using System;
using Newtonsoft.Json;

namespace TimeCard.Core
{
    /// <summary>
    /// 用户打卡分析结果
    /// </summary>
    public class UserResult
    {
        /// <summary>
        /// 开始时
        /// </summary>
        public int? StartHour { get; set; }

        /// <summary>
        /// 开始分
        /// </summary>
        public int? StartMinute { get; set; }

        /// <summary>
        /// 结束时
        /// </summary>
        public int? EndHour { get; set; }

        /// <summary>
        /// 结束分
        /// </summary>
        public int? EndMinute { get; set; }

        /// <summary>
        /// 规则对象
        /// </summary>
        public UserRule Rule { get; set; }

        /// <summary>
        /// 开始时分字符串
        /// </summary>
        [JsonIgnore]
        public string StartString
        {
            get
            {
                //if (Rule == null) return "不考勤";
                if (IsStartUnCard) return "未打卡";
                var result = "正常";
                if (IsStartException)
                {
                    result = "迟到";
                }
                return $"{StartHour:00}:{StartMinute:00}({result})";
            }
        }

        /// <summary>
        /// 结束时分字符串
        /// </summary>
        [JsonIgnore]
        public string EndString
        {
            get
            {
                if (Rule == null) return "不考勤";
                if (IsEndUnCard) return "未打卡";
                var result = "正常";
                if (IsEndException)
                {
                    result = "早退";
                }
                return $"{EndHour:00}:{EndMinute:00}({result})";
            }
        }

        /// <summary>
        /// 开始未打卡
        /// </summary>
        [JsonIgnore]
        public bool IsStartUnCard => !StartHour.HasValue || !StartMinute.HasValue;

        /// <summary>
        /// 迟到
        /// </summary>
        [JsonIgnore]
        public bool IsStartException
        {
            get
            {
                if (Rule == null) return false;
                if (!StartHour.HasValue || !StartMinute.HasValue)
                {
                    return true;
                }
                var ruleTick = new TimeSpan(Rule.StartHour, Rule.StartMinute, 0).Ticks;
                var userTick = new TimeSpan(StartHour.Value, StartMinute.Value, 0).Ticks;
                if (userTick > ruleTick)
                {
                    return true;
                }
                return false;
            }
        }

        /// <summary>
        /// 结束未打卡
        /// </summary>
        [JsonIgnore]
        public bool IsEndUnCard => !EndHour.HasValue || !EndMinute.HasValue;

        /// <summary>
        /// 早退
        /// </summary>
        [JsonIgnore]
        public bool IsEndException
        {
            get
            {
                if (Rule == null) return false;
                if (!EndHour.HasValue || !EndMinute.HasValue)
                {
                    return true;
                }
                var ruleTick = new TimeSpan(Rule.EndHour, Rule.EndMinute, 0).Ticks;
                var userTick = new TimeSpan(EndHour.Value, EndMinute.Value, 0).Ticks;
                if (userTick < ruleTick)
                {
                    return true;
                }
                return false;
            }
        }

        /// <summary>返回表示当前对象的字符串。</summary>
        /// <returns>表示当前对象的字符串。</returns>
        /// <filterpriority>2</filterpriority>
        public override string ToString()
        {
            return $"{StartString} - {EndString}";
        }
    }
}